package com.conferenceroom.vo;

import com.conferenceroom.entity.Orders;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderWithCaltime {
    private Orders order;
    private String countdown; // 倒计时（单位：分钟）
    public static OrderWithCaltime build(Orders order) {
        OrderWithCaltime vo = new OrderWithCaltime();
        vo.setOrder(order);

        if (order.getPaymentStatus() == 0) { // 已预约状态
            Date now = new Date();
            Date createTime = order.getCreateTime();

            // 计算时间差（毫秒）
            long diffInMillis = now.getTime() - createTime.getTime();
            long totalSeconds = TimeUnit.MILLISECONDS.toSeconds(diffInMillis);

            if (totalSeconds <= 1800) { // 30分钟=1800秒
                long remainingSeconds = 1800 - totalSeconds;
                long minutes = remainingSeconds / 60;
                long seconds = remainingSeconds % 60;
                vo.setCountdown(String.format("%02d:%02d", minutes, seconds));
            } else {
                vo.setCountdown(null); // 超过30分钟，不显示倒计时
            }
        } else {
            vo.setCountdown(null);
        }

        return vo;
    }
}
